﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class UpdateHtml : Portal.Interface.WebPart
    {
        protected global::FredCK.FCKeditorV2.FCKeditor FCKeditor1;
        public override string WebPartId
        {
            get
            {
                return "UpdateHtml";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Cập nhật bài viết");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl,System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            string HtmlId = (string)System.Web.HttpContext.Current.Request["HtmlId"];
            HtmlCls HtmlCls = new HtmlBLL().CreateModel(Environments.ActiveLanguage, HtmlId);
            FCKeditor1 = new FredCK.FCKeditorV2.FCKeditor();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CMS.UpdateHtml));
            string SelectLanguageText = 
                "<select id=divSelectLanguage onchange=javascript:DetectHtmlBody('"+HtmlId+"');>";
            string[] Languages = Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";

            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "Html.js") +
                             Portal.Environment.Environments.GetHeaderFormDialog(ResourceBLL.GetText(WebPartId,"UpdateHtmlFormTitle","Cập nhật bài viết"))+
                             "      <table>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "UpdateHtmlSelectLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>"+SelectLanguageText+"</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "UpdateHtmlAlias", "Alias") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtAlias value=\"" + HtmlCls.Alias + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId,"UpdateHtmlSubject","Tiêu đề bài viết") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtSubject value=\"" + HtmlCls.Subject + "\"></td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td colspan=2 class=cssTdFckEditor>";
            pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
            FCKeditor1.Height=400;
            FCKeditor1.Width = 780;
            FCKeditor1.Value = HtmlCls.Body;
            FCKeditor1.BasePath = "fckeditor/";
            pl.Controls.Add(FCKeditor1);
            System.Web.HttpContext.Current.Session["AddHtmlFckEditor"] = FCKeditor1;
            RetHtml =
                             "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveHtml('" + HtmlId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateHtmlSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateHtmlCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>" + Portal.Environment.Environments.GetFooterFormDialog();
            pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
        }



        [AjaxPro.AjaxMethod]
        public string ServerSideHtml(string ActiveLanguage,string HtmlId, string Alias,string Subject, string Body)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                HtmlCls HtmlCls = new HtmlBLL().CreateModel(ActiveLanguage, HtmlId);
                if (Body == null) Body = "";
                if (Subject.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterSubject","Xin vui lòng nhập tiêu đề bài viết!"));
                //if (Body.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"InputBody","Xin vui lòng nhập nội dung bài viết!"));

                Body = System.Web.HttpUtility.HtmlDecode(Body);
                new HtmlBLL().SaveHtml(null, HtmlCls.belongSite, HtmlId, ActiveLanguage,Alias, Subject, Body);
                if(Alias.Trim().ToLower().Equals("InfoBottom".ToLower()))
                {
                    System.Web.HttpContext.Current.Session["InfoBottom"] = Body;
                }
                if (Alias.Trim().ToLower().Equals("InfoTop".ToLower()))
                {
                    System.Web.HttpContext.Current.Session["InfoTop"] = Body;
                }
                return ResourceBLL.GetText(WebPartId,"MessageUpdateOk","Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetLoadDataMsg()
        {
            return ResourceBLL.GetText(WebPartId,"LoadingDataText","Đang đọc dữ liệu ...");
        }


        [AjaxPro.AjaxMethod]
        public string GetBody(string ActiveLanguage,string HtmlId)
        {
            try
            {
                HtmlCls HtmlCls = new HtmlBLL().CreateModel(ActiveLanguage, HtmlId);
                return HtmlCls.Body;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetSubject(string ActiveLanguage, string HtmlId)
        {
            try
            {
                HtmlCls HtmlCls = new HtmlBLL().CreateModel(ActiveLanguage, HtmlId);
                return HtmlCls.Subject;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetInfoSaveMsg(string ActiveLanguage)
        {
            return ActiveLanguage + " - " + Language.ResourceBLL.GetText(WebPartId, "SaveOk", "cập nhật thành công!");
        }
    }
}